package binsearchtree;
/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/13
 * Time: 10:30
 * Description:
 */
class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root ;

    public TreeNode findVal(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if(cur.val < val) {
                cur = cur.right;
            }else if(cur.val == val) {
                return cur;
            }else {
                cur = cur.left;
            }
        }
        return null;
    }

    public void insert(int key) {
        TreeNode treeNode = new TreeNode(key);
        if(root == null) {
            root = treeNode;
            return;
        }
        TreeNode cur = root;
        TreeNode parent = root;
        while (cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val == key) {
                return;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
        if(parent.val < key) {
            parent.right = treeNode;
        }else {
            parent.left = treeNode;
        }
    }


    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;

        while (cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val == key){
                removeNode(parent,cur);
                return;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

    /**
     *
     * @param parent 要删除节点的父亲节点
     * @param cur 需要删除的节点
     */
    private void removeNode(TreeNode parent,TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.left = cur.left;
            }
        } else {
            TreeNode tar = cur.right;
            TreeNode tp = cur;
            while (tar.left != null) {
                tp = tar;
                tar = tar.left;
            }
            cur.val = tar.val;
            if(tar == tp.left) {
                tp.left = tar.right;
            }else {
                tp.right = tar.right;
            }
        }
    }
}
public class Test {
    public static void main(String[] args) {
        BinarySearchTree bina = new BinarySearchTree();
        int[] array = {12,40,3,6,10,8};
        for(int x : array) {
            bina.insert(x);
        }
        bina.remove(12);
        System.out.println("fafa");
    }
}
